/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PengajarUpdateDialog.java
 *
 * Created on 27 Agu 14, 18:59:19
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Pengajar;
import com.ivanbiz.service.GlobalSession;
import com.ivanbiz.service.JTextFieldLimit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class KelasUpdateDialog extends JDialog {

    Kelas kelas;
    KelasDAO kelasDAO = new KelasDAOImpl();
    SimpleDateFormat sdf;
    Pengajar pengajar;

    /**
     * Creates new form PengajarUpdateDialog
     *
     * @param mainFrame
     * @param modal
     */
    public KelasUpdateDialog(MainFrame mainFrame, boolean modal) {
        super(mainFrame, modal);
        initComponents();
        labelKelas.setText("Tambah Kelas Baru");
        sdf = new SimpleDateFormat("yyMMdd");
        textFieldTransaksi.setText(getNoTransaksi());
        jLabel6.setVisible(false);
        jTextFieldNIK.setVisible(false);
    }

    public KelasUpdateDialog(MainFrame mainFrame, boolean modal, Kelas kelas) {
        super(mainFrame, modal);
        initComponents();
        this.kelas = kelas;
        labelKelas.setText("Ubah Kelas");
        textFieldTransaksi.setText(kelas.getTransactionReference());
        jTextFieldNIK.setText(kelas.getNIK());
        jTextFieldDeskripsi.setText(kelas.getDeskripsi());
        pengajar = kelas.getPengajar();
        textPengajar.setText(pengajar.getNama());
        jDateChooserTanggal.setDate(kelas.getTanggalKelas());
        jDateChooserTanggal2.setDate(kelas.getTanggalKelas2());
        jTextFieldTempat.setText(kelas.getTempatKelas());
        jTextAreaAlamat.setText(kelas.getAlamatKelas());
        jLabel6.setVisible(false);
        jTextFieldNIK.setVisible(false);
    }

    private String getNoTransaksi() {
        String noTransaksi = null;
        try {
            noTransaksi = kelasDAO.getLastNoTransaksi();
            if (!"".equals(noTransaksi)) {
                String[] splitNoTransaksi = noTransaksi.split("-");
                int count = Integer.parseInt(splitNoTransaksi[2]);
                count += 1;
                if (count > 99999) {
                    noTransaksi = sdf.format(new Date()) + "-" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", 1);
                } else {
                    noTransaksi = sdf.format(new Date()) + "-" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", count);
                }
            } else {
                noTransaksi = sdf.format(new Date()) + "-" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", 1);
            }
        } catch (Exception ex) {
            Logger.getLogger(KelasUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        return noTransaksi;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelKelas = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNIK = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldDeskripsi = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldTempat = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jDateChooserTanggal = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaAlamat = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        textFieldTransaksi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textPengajar = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jDateChooserTanggal2 = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        labelKelas.setFont(new java.awt.Font("Tahoma", 1, 24));
        labelKelas.setText("Tambah Kelas Baru");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Kelas Reference :");

        jTextFieldNIK.setDocument(new JTextFieldLimit(10));

        jLabel3.setText("Deskripsi :");

        jTextFieldDeskripsi.setDocument(new JTextFieldLimit(50));

        jLabel4.setText("Tempat :");

        jTextFieldTempat.setDocument(new JTextFieldLimit(100));

        jLabel7.setText("Tanggal 1:");

        jLabel8.setText("Alamat :");

        jDateChooserTanggal.setDateFormatString("dd MMM yyyy");

        jTextAreaAlamat.setDocument(new JTextFieldLimit(100));
        jTextAreaAlamat.setRows(5);
        jScrollPane1.setViewportView(jTextAreaAlamat);

        jLabel9.setText("Pengajar :");

        textFieldTransaksi.setDocument(new JTextFieldLimit(50));
        textFieldTransaksi.setEditable(false);

        jLabel6.setText("NIK :");

        textPengajar.setEditable(false);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel10.setText("Tanggal 2:");

        jDateChooserTanggal2.setDateFormatString("dd MMM yyyy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textFieldTransaksi, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldNIK, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldDeskripsi, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(textPengajar, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jDateChooserTanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jDateChooserTanggal2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldTempat, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7)
                            .addComponent(jLabel10)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addGap(0, 269, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldTransaksi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldNIK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldDeskripsi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textPengajar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserTanggal2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTempat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelKelas)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelKelas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-600)/2, 416, 600);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (kelas == null) {
            kelas = new Kelas();
        }
        kelas.setTransactionReference(textFieldTransaksi.getText());
//        kelas.setNIK(jTextFieldNIK.getText());
        kelas.setNIK("");
        kelas.setDeskripsi(jTextFieldDeskripsi.getText());
        kelas.setPengajar(pengajar);
        kelas.setTanggalKelas(jDateChooserTanggal.getDate());
        kelas.setTanggalKelas2(jDateChooserTanggal2.getDate());
        kelas.setStatusKelas("N");
        kelas.setTempatKelas(jTextFieldTempat.getText());
        kelas.setAlamatKelas(jTextAreaAlamat.getText());
        validate(kelas);
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String callPengajar = "callPengajar";
        PengajarDialog pengajarDialog = new PengajarDialog(callPengajar);
        pengajar = pengajarDialog.getPengajar();
        textPengajar.setText(pengajar == null ? "" : pengajar.getNama());
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JButton jButton1;
    private com.toedter.calendar.JDateChooser jDateChooserTanggal;
    private com.toedter.calendar.JDateChooser jDateChooserTanggal2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaAlamat;
    private javax.swing.JTextField jTextFieldDeskripsi;
    private javax.swing.JTextField jTextFieldNIK;
    private javax.swing.JTextField jTextFieldTempat;
    private javax.swing.JLabel labelKelas;
    private javax.swing.JTextField textFieldTransaksi;
    private javax.swing.JTextField textPengajar;
    // End of variables declaration//GEN-END:variables

    private void validate(Kelas kelas) {
        if (kelas == null) {
            JOptionPane.showMessageDialog(this, "Murid tidak boleh null");
        } else if (kelas.getTransactionReference() == null) {
            JOptionPane.showMessageDialog(this, "Transaksi Reference tidak boleh null");
        } else if (kelas.getTransactionReference().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Transaksi Reference tidak boleh kosong");
//        } else if (kelas.getNIK() == null) {
//            JOptionPane.showMessageDialog(this, "NIK tidak boleh null");
//        } else if (kelas.getNIK().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "NIK tidak boleh kosong");
        } else if (kelas.getDeskripsi() == null) {
            JOptionPane.showMessageDialog(this, "Deskripsi tidak boleh null");
        } else if (kelas.getDeskripsi().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Deskripsi tidak boleh kosong");
        } else if (kelas.getPengajar() == null) {
            JOptionPane.showMessageDialog(this, "Pengajar tidak boleh null");
        } else if (kelas.getTanggalKelas() == null) {
            JOptionPane.showMessageDialog(this, "Tanggal tidak boleh null");
        } else if (kelas.getTempatKelas() == null) {
            JOptionPane.showMessageDialog(this, "Tempat tidak boleh null");
        } else if (kelas.getTempatKelas().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Tempat tidak boleh kosong");
        } else if (kelas.getAlamatKelas() == null) {
            JOptionPane.showMessageDialog(this, "Alamat tidak boleh null");
        } else if (kelas.getAlamatKelas().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Alamat tidak boleh kosong");
        } else {
            try {
                kelasDAO.saveOrUpdate(kelas);
                dispose();
            } catch (Exception ex) {
                Logger.getLogger(KelasUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
